import sys
import os
import os.path
import string

def retrieve_field(line, field):
	nano = 1000000000
	
	idx = line.index(field) + len(field)
	result = ""
	count = 0
	while idx < len(line) and line[idx] != " ":
		if line[idx] == ",":
			count += 1
			if count > 1:
				break
		result += line[idx]
		idx += 1
	field = float(result.replace(",","."))
	
	return field/nano

base_dir = '.'
if len(sys.argv) > 1:
	base_dir = sys.argv[1]

for filename in os.listdir(base_dir):
	csv_file = ""
	
	if os.path.isdir(os.path.join(base_dir, filename)):
		continue

	print filename
	file = open(os.path.join(base_dir, filename), 'r')
	content = file.readlines()
	file.close()
	
	thread_id = 0
	for line in content:
		mean,sd = retrieve_field(line, "mean="), retrieve_field(line, "sd=")
		high = mean + sd
		low = mean - sd
		if (low < 0):
			low = 0
		thread_name = "Tread-" + str(thread_id)
		csv_file += thread_name + "," + str(mean) + "," + str(high) + "," + str(low) + "," + str(mean) + "\n"
		thread_id += 1
		
	file = open(os.path.join(base_dir, filename.replace(".txt", ".csv")), "w")
	file.write(csv_file)
	file.close()